//
//  SAShopSaleCell.m
//  saasRetail
//
//  Created by 黄志刚 on 2021/4/13.
//

#import "SAShopSaleCell.h"
#import "SASalesOrderViewListModel.h"
#import "SAVipModel.h"

@interface SAShopSaleCell ()

@property (nonatomic, strong) UILabel *titleLab;
@property (nonatomic, strong) UILabel *colourSizeLab;
@property (nonatomic, strong) UILabel *numLab;
@property (nonatomic, strong) UIImageView *iconImageV;
@property (nonatomic, strong) UILabel *priceLab;
@property (nonatomic, strong) UILabel *discountPriceLab;
@property (nonatomic, strong) CAShapeLayer *shapeLayer;
@property (nonatomic, strong) CAShapeLayer *bgLayer;
@property (nonatomic, strong) UILabel *shapeLab;

@end

@implementation SAShopSaleCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

-(void)setVipModel:(SAVipModel *)vipModel {
    _vipModel = vipModel;
}

-(void)setModel:(SASalesOrderViewListModel *)model {
    _model = model;
    //处理赠送类型
//    if (model.isGive == YES) {
//        model.discountRate = @"0";
//    }
    
    [self.iconImageV sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"sale_bule_defaul"]];
    self.titleLab.text = model.productName;
    NSMutableArray *colourSizeMarr = [NSMutableArray array];
    NSInteger num = 0;
    for (SASalesOrderToPendSkuList *list in model.toPendSkuAttrs) {
        NSString *str = [NSString stringWithFormat:@"%@/%@",list.colourName,list.sizeName];
        NSInteger stock = [list.stock integerValue];
        if (stock != 0) {
            num += stock;
            [colourSizeMarr addObject:str];
        }
    }
    self.colourSizeLab.text = [colourSizeMarr componentsJoinedByString:@"、"];
    self.numLab.text = [NSString stringWithFormat:@"%zd",num];

    //确认使用哪种百分比
    CGFloat percentage = [model.discountRate floatValue] / 10000;
    CGFloat discountRate = [model.discountRate floatValue] / 10000;
    if (self.isIndividualDiscount == YES && self.vipModel.memberId.length == 0) {
        percentage = 1.0;
        discountRate = 1.0;
    }

    //是否用原价来计算
    BOOL isRetailPrice = [model.activityPriceType isEqualToString:@"0"] && model.isOpenActivity == YES;
    
    self.shapeLayer.hidden = false;
    self.shapeLab.hidden = false;
    self.bgLayer.hidden = false;
    
    if (model.isGive == YES) {
        percentage = 0;
    } else if (isRetailPrice == YES) {
        percentage = 1;
        self.shapeLayer.hidden = true;
        self.shapeLab.hidden = true;
        self.bgLayer.hidden = true;
    }else if (![model.priceType isEqualToString:@"-1"]) {
        percentage = model.percentage;
    }else {
        if (self.vipModel == nil) {
            percentage = discountRate;
            model.salePrice = [model.retailPrice floatValue] * percentage;
            model.percentage = percentage;
        }else {
            //会员折上折
            NSString *isSuperposition = self.vipModel.isSuperposition;
            //参与活动时，按活动来走
            if (model.isOpenActivity == YES) {
                isSuperposition = model.memberDiscountStacking;
            }
            
            CGFloat memberDiscount =  [self.vipModel.memberDiscount floatValue] / 100;
            
            if ([isSuperposition isEqualToString:@"1"]) {
                percentage = discountRate * memberDiscount;
            }else {
                if (model.isOpenActivity == YES && [model.memberDiscountStacking isEqualToString:@"0"]) {
                    percentage = discountRate;
                }else if (memberDiscount > discountRate) {
                    percentage = discountRate;
                }else{
                    percentage = memberDiscount;
                }
            }
            model.salePrice = [model.retailPrice floatValue] * percentage;
            model.percentage = percentage;
        }
    }
    self.shapeLayer.strokeEnd = percentage;
    if (model.isGive == YES) {
        self.shapeLayer.strokeEnd = 1;
        self.shapeLab.text = @"赠送";
    }else if (percentage == 1) {
        self.shapeLab.text = @"100%";
    }else{
        self.shapeLab.text = [NSString stringWithFormat:@"%0.2f%%",percentage * 100];
    }
    
    //默认是使用百分比,如果手输入价格 priceType = 1
    if ([model.priceType isEqualToString:@"0"] && isRetailPrice != YES) {
        NSInteger salePrice = [[NSString stringWithFormat:@"%0.f",model.salePrice] integerValue];
        NSInteger retailPrice = [model.retailPrice integerValue];
        if (salePrice == retailPrice) {
            self.discountPriceLab.hidden = YES;
            self.priceLab.frame = SARectMake(240, 0, 72, 55);
            NSString *textContent = [NSString stringWithFormat:@"%0.2lf",round([model.retailPrice doubleValue] * num)/100.f];
            NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
            model.totalSalePrice = [model.retailPrice doubleValue]/100.f * num;
            model.singleSalePrice = [NSString stringWithFormat:@"%0.2lf",round([model.retailPrice doubleValue])/100.f];
            textDict[NSForegroundColorAttributeName] = kBlackColor;
            textDict[NSFontAttributeName] = kFont(11);
            self.priceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
        }else {
            self.discountPriceLab.hidden = NO;
            double discountPrice = salePrice * num;
            double textContentPrice = retailPrice * num;
            double singlePrice = salePrice * 1;
            self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2lf",round(discountPrice) / 100];
            model.totalSalePrice = discountPrice / 100;
            model.singleSalePrice = [NSString stringWithFormat:@"%0.2lf",round(singlePrice)/100.f];
            self.priceLab.frame = SARectMake(240, 29, 72, 15);
            NSString *textContent = [NSString stringWithFormat:@"原价%0.2lf",round(textContentPrice) / 100];
            NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
            textDict[NSStrikethroughColorAttributeName] = kPinkColor;
            textDict[NSForegroundColorAttributeName] = kLightgGrayColor;
            textDict[NSFontAttributeName] = kFont(11);
            textDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
            textDict[NSBaselineOffsetAttributeName] = @(NSUnderlineStyleSingle);
            self.priceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
        }
    }else {
        if (percentage == 1) {
            self.discountPriceLab.hidden = YES;
            self.priceLab.frame = SARectMake(240, 0, 72, 55);
            NSString *textContent = [NSString stringWithFormat:@"%0.2lf",round([model.retailPrice doubleValue] * num)/100.f];
            NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
            model.totalSalePrice = [model.retailPrice doubleValue]/100.f * num;
            model.singleSalePrice = [NSString stringWithFormat:@"%0.2lf",round([model.retailPrice doubleValue])/100.f];
            textDict[NSForegroundColorAttributeName] = kBlackColor;
            textDict[NSFontAttributeName] = kFont(11);
            self.priceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
        }else {
            self.discountPriceLab.hidden = NO;
            NSInteger newRetailPrice = [model.retailPrice integerValue];
            NSInteger newPercentage = [[NSString stringWithFormat:@"%0.1f%%",percentage * 10000] integerValue];
            double discountPrice = newRetailPrice * num * newPercentage;
            double singlePrice = newRetailPrice * 1 * newPercentage;
            double textContentPrice = newRetailPrice * num;
            self.discountPriceLab.text = [NSString stringWithFormat:@"%0.2lf",round(discountPrice/10000)/100];
            model.totalSalePrice = discountPrice / 1000000;
            model.singleSalePrice = [NSString stringWithFormat:@"%0.2lf",round(singlePrice/10000)/100];
            self.priceLab.frame = SARectMake(240, 29, 72, 15);
            NSString *textContent = [NSString stringWithFormat:@"原价%0.2lf",round(textContentPrice) / 100];
            NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
            textDict[NSStrikethroughColorAttributeName] = kPinkColor;
            textDict[NSForegroundColorAttributeName] = kLightgGrayColor;
            textDict[NSFontAttributeName] = kFont(11);
            textDict[NSStrikethroughStyleAttributeName] = [NSNumber numberWithInteger:NSUnderlineStyleSingle];
            textDict[NSBaselineOffsetAttributeName] = @(NSUnderlineStyleSingle);
            self.priceLab.attributedText = [[NSAttributedString alloc] initWithString:textContent attributes:textDict];
        }
    }
}

-(void)clickedTap {
    if (self.model.isGive == YES) {
        return;
    }
    !self.discountBlock ?: self.discountBlock();
}

#pragma mark - initUI
-(void)initUI {
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(0, 0, kBaseScreenWidth, 1)];
    line.backgroundColor = kLineColor;
    [self.contentView addSubview:line];
    
    UIImageView *iconImageV = [[UIImageView alloc] initWithFrame:SARectMake(20, 10, 30, 35)];
    iconImageV.image = [UIImage imageNamed:@"sale_bule_defaul"];
    [self.contentView addSubview:iconImageV];
    iconImageV.contentMode = UIViewContentModeScaleAspectFit;
    self.iconImageV = iconImageV;
    
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.colourSizeLab];
    [self.contentView addSubview:self.numLab];
    [self.contentView addSubview:self.priceLab];
    [self.contentView addSubview:self.discountPriceLab];
    
    UIView *shapeView = [[UIView alloc] initWithFrame:SARectMake(kBaseScreenWidth - 53, 8, 40, 40)];
    shapeView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:shapeView];
    
    CAShapeLayer *bgLayer = [CAShapeLayer layer];
    self.bgLayer = bgLayer;
    bgLayer.frame = CGRectMake(0, 0, kScaleNum(40), kScaleNum(40));//设置Frame
    // bgLayer.position = self.view.center;//居中显示
    bgLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色=透明色
    bgLayer.lineWidth = kScaleNum(1);//线条大小
    bgLayer.strokeColor = kColor(220, 220, 220, 1).CGColor;//线条颜色
    bgLayer.strokeStart = 0.f;//路径开始位置
    bgLayer.strokeEnd = 1.f;//路径结束位置
    UIBezierPath *circle = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, kScaleNum(40), kScaleNum(40))];
    bgLayer.path = circle.CGPath;//设置bgLayer的绘制路径为circle的路径
    [shapeView.layer addSublayer:bgLayer];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = CGRectMake(0, 0, kScaleNum(40), kScaleNum(38));//设置shapeLayer的尺寸和位置
    shapeLayer.fillColor = [UIColor clearColor].CGColor;//填充颜色为ClearColor
    
    //设置线条的宽度和颜色
    shapeLayer.lineWidth = kScaleNum(1);
    shapeLayer.strokeColor = kPinkColor.CGColor;
    shapeLayer.strokeStart = 0;
    //创建出圆形贝塞尔曲线
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, kScaleNum(40), kScaleNum(40))];
    //让贝塞尔曲线与CAShapeLayer产生联系
    shapeLayer.path = circlePath.CGPath;
    //添加并显示
    [shapeView.layer addSublayer:shapeLayer];
    self.shapeLayer = shapeLayer;
    CGAffineTransform transform = CGAffineTransformIdentity;
    shapeView.transform = CGAffineTransformRotate(transform, -M_PI);
    self.shapeLayer.strokeEnd = 1;
    
    [self.contentView addSubview:self.shapeLab];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedTap)];
    [self.shapeLab addGestureRecognizer:tap];
}

-(UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:SARectMake(60, 11, 130, 15)];
        _titleLab.textColor = kBlackColor;
        _titleLab.font = kFont(13);
    }
    return _titleLab;
}

-(UILabel *)colourSizeLab {
    if (!_colourSizeLab) {
        _colourSizeLab = [[UILabel alloc] initWithFrame:SARectMake(60, 29, 130, 15)];
        _colourSizeLab.textColor = kLightgGrayColor;
        _colourSizeLab.font = kFont(13);
    }
    return _colourSizeLab;
}

-(UILabel *)numLab {
    if (!_numLab) {
        _numLab = [[UILabel alloc] initWithFrame:SARectMake(195, 0, 40, 55)];
        _numLab.textColor = kBlackColor;
        _numLab.font = kFont(13);
        _numLab.textAlignment = NSTextAlignmentLeft;
    }
    return _numLab;
}

-(UILabel *)priceLab {
    if (!_priceLab) {
        _priceLab = [[UILabel alloc] initWithFrame:SARectMake(240, 29, 72, 15)];
        _priceLab.textColor = kBlackColor;
        _priceLab.font = kFont(11);
        _priceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _priceLab;
}

-(UILabel *)discountPriceLab {
    if (!_discountPriceLab) {
        _discountPriceLab = [[UILabel alloc] initWithFrame:SARectMake(240, 11, 72, 15)];
        _discountPriceLab.textColor = kBlackColor;
        _discountPriceLab.font = kFont(11);
        _discountPriceLab.textAlignment = NSTextAlignmentCenter;
    }
    return _discountPriceLab;
}

-(UILabel *)shapeLab {
    if (!_shapeLab) {
        _shapeLab = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 54, 10, 41, 38)];
        _shapeLab.textColor = kPinkColor;
        _shapeLab.font = kFont(11);
        _shapeLab.textAlignment = NSTextAlignmentCenter;
        _shapeLab.userInteractionEnabled = YES;
    }
    return _shapeLab;
}

@end
